﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query for the thrift call get_slice on subcolumns of a supercolumns.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class ThriftSubSliceCounterQuery<K, SN, N> : AbstractSliceQuery<K, N, long, CounterSlice<N>>, SubSliceCounterQuery<K, SN, N>
    {
        public ThriftSubSliceCounterQuery(Keyspace k, Serializer<K> keySerializer, 
            Serializer<SN> sNameSerializer, Serializer<N> nameSerializer) :
            base(k, keySerializer, nameSerializer, LongSerializer.Instance)
        {
            if (sNameSerializer == null)
            {
                throw new NullReferenceException("SuperNameSerializer cannot be null");
            }
            SuperNameSerializer = sNameSerializer;
        }
        public K Key
        {
            get;
            set;
        }
        private SN SuperColumn
        {
            get;
            set;
        }
        private Serializer<SN> SuperNameSerializer
        {
            get;
            set;
        }
        public SubSliceCounterQuery<K,SN,N> SetKey(K key) 
        {
            Key = key;
            return this;
        }
        public SubSliceCounterQuery<K,SN,N> SetSuperColumn(SN superColumn) 
        {
            SuperColumn = superColumn;
            return this;
        }
        public override string ToString()
        {
            return "SubSliceCounterQuery(" + Key + "," + SuperColumn + "," + ToStringInternal() + ")";
        }
        public new SubSliceCounterQuery<K, SN, N> SetColumnNames(List<N> columnNames)
        {
            return (SubSliceCounterQuery<K, SN, N>) base.SetColumnNames(columnNames);
        }
        public new SubSliceCounterQuery<K, SN, N> SetRange(N start, N finish, bool reversed, int count)
        {
            return (SubSliceCounterQuery<K, SN, N>) base.SetRange(start, finish, reversed, count);
        }
        public new SubSliceCounterQuery<K, SN, N> SetColumnFamily(string cf)
        {
            return (SubSliceCounterQuery<K, SN, N>) base.SetColumnFamily(cf);
        }
        public override QueryResult<CounterSlice<N>> Execute()
        {
            if (Key == null || SuperColumn == null)
            {
                throw new NullReferenceException("Key, and SuperColumn cannot be null");
            }
            Func<KeyspaceService, CounterSliceImpl<N>> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                List<CounterColumn> thriftRet = ks.GetCounterSlice(KeySerializer.ToBytes(Key), columnParent, GetPredicate());
                return new CounterSliceImpl<N>(thriftRet, ColumnNameSerializer);
            };
            return new QueryResultImpl<CounterSlice<N>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<CounterSlice<N>>(x)), this);
        }
    }
}
